package korkd.tower;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.path.Path;

import android.view.MotionEvent;

class Army extends AnimatedSprite
{
	// Attributes
	private boolean controlled;
	private int count;
	private int level;
	
	private Army mArmy;
	private Player mPlayer;
	private Castle mDestCastle;
	private Castle mSourceCastle;
	private ArmyPool mArmyPool;
	private TMXTiledMap mTMXTiledMap;

	// Constructor
	public Army(float pX, float pY, TiledTextureRegion pTextureRegion, Player pPlayer, ArmyPool pArmyPool, TMXTiledMap pTMXTiledMap) {
		super(pX, pY, pTextureRegion);
		this.setControlled(false);
		this.setLevel(1);
		mPlayer = pPlayer;
		mArmy = this;
		mDestCastle = null;
		mSourceCastle = null;
		mArmyPool = pArmyPool;
		mTMXTiledMap = pTMXTiledMap;
		this.setSize(40, 40);
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
	{
		if(pTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
		{
		}
		if(pTouchEvent.getAction() == MotionEvent.ACTION_UP)
		{
		}
		return true;
	}
	
	public void move()
	{
		final Path path = mSourceCastle.getPath(mDestCastle);
		int[] TileDimensions =  {mTMXTiledMap.getTileWidth(), mTMXTiledMap.getTileHeight()};
		
		this.registerEntityModifier(new AStarPathModifier(path.getLength()/2, path, TileDimensions , new AStarPathModifier.IAStarPathModifierListener()
		{
	        @Override
	        public void onPathWaypointStarted(AStarPathModifier pPathModifier, final IEntity pEntity, int pWaypointIndex) {
        	}

			@Override
			public void onPathStarted(AStarPathModifier pPathModifier,
					IEntity pEntity) {
				
			}

			@Override
			public void onPathWaypointFinished(AStarPathModifier pPathModifier,
					IEntity pEntity, int pWaypointIndex) {
				
			}

			@Override
			public void onPathFinished(AStarPathModifier pPathModifier,
					IEntity pEntity) {
				
				mArmyPool.recyclePoolItem(mArmy);
        		mDestCastle.TDGame(mPlayer);
        		mSourceCastle = null;
        		mDestCastle = null;
				
			}

			@Override
			public void onNextMoveLeft(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
            	mArmy.animate(new long[]{150, 150, 150}, 3, 5, true);
			}

			@Override
			public void onNextMoveUp(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
            	mArmy.animate(new long[]{150, 150, 150}, 9, 11, true);
			}

			@Override
			public void onNextMoveRight(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
				mArmy.animate(new long[]{150, 150, 150}, 6, 8, true);
			}

			@Override
			public void onNextMoveDown(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
				mArmy.animate(new long[]{150, 150, 150}, 0, 2, true);
			}
		}));
	}
	
	// BEGIN Setters and Getters
	// ****************************************************************
	public void setControlled(boolean controlled) {
		this.controlled = controlled;
	}

	public boolean isControlled() {
		return controlled;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getLevel() {
		return level;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}
	
	public boolean isDestSet()
	{
		boolean result = true;
		
		if(mDestCastle == null)
		{
			result = false;
		}
		
		return result;
	}
	
	public boolean isSourceSet()
	{
		boolean result = true;
		
		if(mSourceCastle == null)
		{
			result = false;
		}
		
		return result;
	}

	public Castle getDestCastle() {
		return mDestCastle;
	}

	public void setDestCastle(Castle mDestCastle) {
		this.mDestCastle = mDestCastle;
	}

	public Castle getSourceCastle() {
		return mSourceCastle;
	}

	public void setSourceCastle(Castle mSourceCastle) {
		this.mSourceCastle = mSourceCastle;
	}
}